package com.wuqi.dev.mvc;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.wuqi.dev.mvc.entity.GankEntity;
import com.wuqi.dev.mvc.http.HttpManager;
import com.wuqi.dev.mvc.presenter.Presenter;
import com.wuqi.dev.mvc.ui.GankAdapter;
import com.wuqi.dev.mvc.ui.IView;

public class MainActivity extends AppCompatActivity implements IView {

    private final static String PATH = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1";

    private ProgressDialog mDialog;
    private ListView mListView;
    private GankAdapter mAdapter;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.list_view);
        mAdapter = new GankAdapter(this);
        mListView.setAdapter(mAdapter);
        // V层只需要了解如何使用数据和何时触发某个事件
        //

        mDialog = new ProgressDialog(this);
        mDialog.setTitle("开始网络请求");
        mDialog.setMessage("请求中...");
        Presenter presenter = new Presenter(this);
        presenter.startRequest(PATH);


    }

    @Override
    public void showDialog () {
        mDialog.show();
    }

    @Override
    public void dismissDialog () {
        mDialog.dismiss();
    }

    @Override
    public void success (GankEntity gank) {
        mAdapter.addAll(gank.getResults());
    }

    @Override
    public void failed (Throwable ex) {
        Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
